package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/guild/entity"
	"time"
)

func CountByBossRecordRoleID(dB *gorm.DB, guildID, bossID int32) int {

	var data = struct {
		C int
	}{}

	var now = time.Now()
	var dateTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	if err := dB.Table("boss_records").Select("COUNT(role_id) AS c").Where("guild_id = ?", guildID).Where("boss_id = ?", bossID).Where("created_at > ?", dateTime).
		Group("role_id").Scan(&data).Error; err != nil {
		panic(err)
	}

	return data.C
}

func DelBossRecord(dB *gorm.DB) {

	var now = time.Now()
	var dateTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)

	dB.Where("created_at < ?", dateTime).Delete(entity.BossRecord{})
}
